<?php
namespace app\admin\controller;
use think\Request;
use think\Db;
use think\Config;

class SupplierApply extends Base
{
	/**
	 * 供应商列表
	 * @return [type] [description]
	 */
	function index(){
		$keywords = Request::instance()->param("keywords",'');
		$apply_status = Request::instance()->param("apply_status",'','intval');
        $query= Db::name('supplier_apply');

        if ( !empty($keywords) ) {
            $query->where('supplier_name|charge|charge_phone', 'like', '%'.$keywords.'%');
        }

        if (!empty($apply_status)){
            $query->where('apply_status',$apply_status);
        }
        $data['list'] = $query->paginate('',false,['query'=>Request()->param()]);
		return $this->fetch('index',$data);
	}

	
/**
	 * 审核详情
	 * @return [type] [description]
	 */
	function info(){
		
		if (Request()->isPost()) { //审核处理
			$params = Request()->only('apply_id,apply_status,refuse_reason');


			if ($params['apply_status'] == -1 && empty($params['refuse_reason'])) {
			    $this->error('请填写拒绝理由！');
			}

            Db::startTrans();
            
            $res = Db::name('supplier_apply')->where(['apply_id'=>$params['apply_id']])->setField('apply_status',$params['apply_status']);//更新状态

            $apply_info = Db::name('supplier_apply')->where(['apply_id'=>$params['apply_id']])->find();

            if ($params['apply_status'] == -1) { //更新拒绝理由并且发送短信提醒拒绝原因
            	$res_2 = Db::name('supplier_apply')->where(['apply_id'=>$params['apply_id']])->setField('refuse_reason',$params['refuse_reason']);
            	$res_3 = true;
            	$res_4 = true;
            	$res_5 = true;
                $res_6 = Db::name('supplier_apply')->where(['apply_id'=>$params['apply_id']])->setField('admin_id',$this->uid);
            	//to do 
//            	$this->sendTmp($apply_info,$params['refuse_reason']);
            }elseif($params['apply_status'] == 2){//copy入表并且发送短信提醒审核通过

            	$res_2 = true;

            	if (Db::name('supplier')->where(['supplier_name'=>$apply_info['supplier_name']])->find()) {
            		$this->error('供应商已存在');
            	}

            	if (Db::name('supplier_user')->where(['username'=>$apply_info['username']])->find()) {
            		$this->error('管理员账号已存在');
            	}

            	$dataS = [
            		'supplier_name'=>$apply_info['supplier_name'],
            		'charge'=>$apply_info['charge'],
            		'charge_phone'=>$apply_info['charge_phone'],
            		'business'=>$apply_info['business'],
            		'idcard'=>$apply_info['idcard'],
            		'idcard_zp'=>$apply_info['idcard_zp'],
            		'idcard_fp'=>$apply_info['idcard_fp'],
            		'supplier_status'=>1,
                    'admin_id'=>$this->uid,
                    'channel'=>2
            	];

            	$res_3 = Db::name('supplier')->insert($dataS);

            	$supplier_id =Db::name('supplier')->getLastInsID();

                //生成随机密码 修改密码 发送给用户提醒登录
                $password = get_rand_pass(6);

                if (empty($password)){
                    $this->error('密码生成失败！');
                }

            	$dataSU =[
            		'supplier_id'=>$supplier_id,
            		'username'=>$apply_info['username'],
            		'password'=>md5($password),
            		'is_super'=> 1	
            	];

            	$res_4 = Db::name('supplier_user')->insert($dataSU);

            	$res_5 = Db::name('supplier_apply')->where(['apply_id'=>$params['apply_id']])->setField('supplier_id',$supplier_id);

            	$res_6 = Db::name('supplier_apply')->where(['apply_id'=>$params['apply_id']])->setField('admin_id',$this->uid);

            	//to do 

            	$this->sendTmp($apply_info,$password);
            }

			if ($res!==false && $res_2!==false && $res_3!==false && $res_4!==false && $res_5!==false && $res_6!==false) {
				Db::commit();
				$this->success('操作成功','','refresh');
			}else{
				Db::rollback();
				$this->error('操作失败');
			}

		}else{
			$params = Request()->param();
			!isset($params['apply_id']) && $this->error('参数错误');
			$info = Db::name('supplier_apply')->where(['apply_id'=>$params['apply_id']])->find();
			if (!empty($info['admin_id'])){
                $admin_info = Db::name('admins')->where(['id'=>$info['admin_id']])->find();
                $info['admin_text'] = '['.$admin_info['username'].']'.$admin_info['name'].'('.$admin_info['phone'].')';
            }else{
			    $info['admin_text'] = '暂无审核人';
            }

            $data = ['info'=>$info];
			return $this->fetch('info',$data);
		}
	}


	 /**
     * 发送模板消息
     * @param  [type] $info [description]
     * @return [type]        [description]
     */
    public function sendTmp($info,$password){
        import('Alidayu.TopSdk');
        import('Alidayu.top.TopClient');
        import('Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
        $appkey="23535194";
        $secret="48953173553ad6ad6b306138f518d7f1";

        $c = new \TopClient();
        $c ->appkey = $appkey ;
        $c ->secretKey = $secret ;
        $req = new \AlibabaAliqinFcSmsNumSendRequest();
        $req ->setExtend( "" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "速冻在线" );
        $req ->setSmsParam("{name:'".$info['supplier_name']."',account:'".$info['username']."',password:'".$password."'}");
        $req ->setRecNum( $info['charge_phone'] );
        $req ->setSmsTemplateCode( "SMS_116480065" );
        $re = $c ->execute( $req );
    }
}